<?php

require_once 'category.admin.inc';
require_once 'brand.admin.inc';
require_once 'price.admin.inc';
/**
 * lay toan bo group cua danh muc
 * @param int $cate_id
 * @return group cua danh muc, header, pager
 * @author anhmantk
 * @data Dec 27, 2011
 */
function getAllGroupByCateId($cate_id) {
    $header = array(
        array('data' => 'Tên nhóm'),
        array('data' => t('Thuộc tính'), 'width' => '70'),
        array('data' => 'Độ ưu tiên', 'field' => 'group_position', 'sort' => 'asc', 'width' => '81'),
        array('data' => 'Trạng thái', 'width' => '100'),
        array('data' => t('Khóa'), 'width' => '45'),
    );

    $where = ' AND cate_id = ' . $cate_id;

    $query = 'SELECT * FROM {category_filter_group} WHERE 1 = 1 ' . $where . ' ' . tablesort_sql($header);
    $count_query = 'SELECT COUNT(group_id) FROM {category_filter_group} WHERE 1 = 1 ' . $where;
    $result = pager_query($query, LIMIT_PAGER, 0, $count_query);
    
    while (($obj = db_fetch_object($result)) != null) {
        $rs[] = $obj;
    }

    $pager = theme('pager');

    return array($header, $rs, $pager);
}

/**
 * lay thong tin group theo id va cate_id
 * @param int $group_id
 * @param int $cate_id
 * @return stdClass thong tin danh muc
 * @author anhmantk
 * @date Dec 27, 2011
 */
function getFilterGroupById($group_id, $cate_id) {
    $query = "SELECT * FROM {category_filter_group} WHERE group_id = %d AND cate_id = %d";
    return db_fetch_object(db_query($query, $group_id, $cate_id));
}

/**
 * insert multi group cho danh muc
 * @param array $params
 * @return boolean
 * @author anhmantk
 * @date Dec 27, 2011
 */
function insertFilterGroup($params = array()) {
    if (count($_POST['group_name']) > 0) {
        $query = "INSERT INTO {category_filter_group} (`cate_id`, `group_name`, `group_position`, `group_status`) VALUES ";
        $sql = "";
        $variables = array();
        foreach ($_POST['group_name'] as $key => $value) {
            if (!trim($value)) { // neu khong co du lieu thi bo qua
                continue;
            }

            $sql .= " (%d, '%s', %d, %d),";
            $variables[] = $_POST['cate_id'];
            $variables[] = check_html($value);
            $variables[] = $_POST['group_position'][$key];
            $variables[] = $_POST['group_status'][$key];
        }

        if ($sql) {
            $query .= rtrim($sql, ',');
            return db_query($query, $variables);
        } else {
            return false;
        }
    }
    return true;
}

/**
 * update filter group
 * @param string $group_name
 * @param int $group_id
 * @return boolean
 * @author anhmantk
 * @date Dec 28, 2011
 */
function updateFilterGroup($group_name, $group_id) {
    $query = "UPDATE {category_filter_group} SET group_name = '%s' WHERE group_id = %d";
    return db_query($query, array(check_html($group_name), $group_id));
}

/**
 * block group voi quyen (admin block)
 * @param int $group_id
 * @param int $cate_id
 * @param int $group_status
 * @return boolean
 * @author anhmantk
 * @date Dec 28, 2011
 */
function blockFilterGroupById($group_id, $cate_id, $group_status) {
    $query = "UPDATE {category_filter_group} SET group_status = %d WHERE group_id = %d AND cate_id = %d";
    return db_query($query, $group_status, $group_id, $cate_id);
}

/**
 * insert multi filer name
 * @param array $params 
 * @author anhmantk
 * @date Dec 28, 2011
 */
function insertFilterName($params) {
    if (count($_POST['filter_name']) > 0) {
        $query = "INSERT INTO {category_filter_name} (`cate_id`, `filter_name`, `filter_group`, `filter_type`, `filter_status`) VALUES ";
        $sql = "";
        $variables = array();
        foreach ($_POST['filter_name'] as $key => $value) {
            if (!trim($value)) {
                continue;
            }
            $sql .= " (%d, '%s', %d, %d, %d),";
            $variables[] = $_POST['cate_id'];
            $variables[] = check_html($value);
            $variables[] = $_POST['group_id'];
            $variables[] = $_POST['filter_type'][$key];
            $variables[] = $_POST['filter_status'][$key];
        }
        if ($sql) {
            $query .= rtrim($sql, ',');
            return db_query($query, $variables);
        } else {
            return false;
        }
    }
    return true;
}

/**
 * lay toan bo thuoc tinh cua nhom filter
 * @param int $group_id
 * @param int $cate_id
 * @return array thuoc tinh cua nhom
 * @author anhmantk
 * @date Dec 28, 2011
 */
function getAllFilterNameByGroupId($group_id, $cate_id) {
    $query = "SELECT * FROM {category_filter_name} WHERE filter_group = %d AND cate_id = %d";
    $result = db_query($query, array($group_id, $cate_id));    
    while (($obj = db_fetch_object($result)) != null) {
        $rs[] = $obj;
    }
    return $rs;
}

/**
 * update filter name
 * @param string $filter_name
 * @param int $filter_id
 * @return boolean
 * @author anhmantk
 * @date Dec 28, 2011
 */
function updateFilterName($filter_name, $filter_id) {
    $query = "UPDATE {category_filter_name} SET filter_name = '%s' WHERE filter_id = %d";
    return db_query($query, array(check_html($filter_name), $filter_id));
}

/**
 * lay thong tin filter name theo id
 * @param int $filter_id
 * @return stdClass thong tin filer name
 * @author anhmantk
 * @date Dec 28, 2011
 */
function getFilterNameById($filter_id) {
    $sql = "SELECT * FROM {category_filter_name} WHERE filter_id = %d";
    return db_fetch_object(db_query($sql, $filter_id));
}

/**
 * block or unlock filter name
 * Neu filter_status: 1 thi lock
 * Neu filter_status: 0 thi unlock
 * @param int $filter_id
 * @param int $filter_status
 * @return string lock|unlock
 * @author anhmantk
 * @date Dec 28, 2011
 */
function blockUnblockFilterNameById($filter_status, $filter_id) {
    $query = "UPDATE {category_filter_name} SET filter_status = '%d' WHERE filter_id = %d";
    return db_query($query, array($filter_status, $filter_id));
}

/**
 * lay toan bo gia tri cua thuoc tinh
 * @param int $filter_id
 * @return string serialize
 * @author anhmantk
 * @date Dec 28, 2011 
 */
function getPropertyOfFilterName($filter_id) {
    $query = "SELECT property_name FROM {category_filter_property} WHERE filter_id = " . $filter_id;    
    return db_result(db_query($query, $filter_id));
}

/**
 * them moi gia tri cho filter name
 * @param int $filter_id
 * @param string $value 
 * @return boolean
 * @author anhmantk
 * @date Dec 28, 2011
 */
function insertProperty($filter_id, $value) {
    $sql = "SELECT COUNT(filter_id) FROM {category_filter_property} WHERE filter_id = %d";
    $count = db_result(db_query($sql, $filter_id));
    if ($count > 0) { //neu da co gia tri roi thi update        
        return updateProperty($filter_id, $value);
    }
    $query = "INSERT INTO {category_filter_property} (filter_id, property_name) VALUES (%d ,'%s')";
    return db_query($query, array($filter_id, $value));
}

/**
 * update filter property
 * @param int $filter_id
 * @param string $value serialize
 * @return boolean
 * @author anhmantk
 * @date Dec 29, 2011
 */
function updateProperty($filter_id, $value) {
    $query = "UPDATE {category_filter_property} SET property_name = '%s' WHERE filter_id = %d";
    return db_query($query, array($value, $filter_id));
}
